package com.wolfsea.launcherdemo
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.get

/**
 *@desc  TopContentLayoutBottom
 *@author liuliheng
 *@time 2023/1/30  16:30
 **/
class TopContentLayoutBottom @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : ViewGroup(context, attributeSet) {

    private var contentViewGroup: TopContentViewGroup? = null

    private var downX = 0
    private var downY = 0
    private var flagDownX = 0
    private var flagDownY = 0

    private val tvFlag: AppCompatTextView

    init {
        inflate(context, R.layout.top_content_bottom_layout, this)

        tvFlag = findViewById(R.id.tv_flag)
        tvFlag.setOnClickListener {
            context.showToast("CircleRed")
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        var totalWidth = 0
        var totalHeight = 0
        for (i in 0.until(childCount)) {
            val child = get(i)
            measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0)
            val childWidth = child.measuredWidth
            val childHeight = child.measuredHeight
            val childLayoutParams = child.layoutParams as MarginLayoutParams
            totalWidth = Math.max(
                0,
                childWidth + childLayoutParams.leftMargin + childLayoutParams.rightMargin
            )
            totalHeight += childHeight + childLayoutParams.topMargin + childLayoutParams.bottomMargin
        }
        setMeasuredDimension(
            MeasureSpec.makeMeasureSpec(
                totalWidth,
                MeasureSpec.EXACTLY
            ) - paddingStart - paddingEnd,
            MeasureSpec.makeMeasureSpec(
                totalHeight,
                MeasureSpec.EXACTLY
            ) - paddingTop - paddingBottom
        )
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        for (i in 0.until(childCount)) {
            val child = get(i)
            val childMeasuredWith = child.measuredWidth
            val childMeasuredHeight = child.measuredHeight
            val layoutParams = child.layoutParams as MarginLayoutParams
            child.layout(
                layoutParams.leftMargin,
                layoutParams.topMargin,
                layoutParams.leftMargin + childMeasuredWith,
                layoutParams.topMargin + childMeasuredHeight
            )
        }
    }

    override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams {
        return MarginLayoutParams(context, attrs)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        when (ev?.action) {
            MotionEvent.ACTION_DOWN -> {
                parent.requestDisallowInterceptTouchEvent(true)
                downX = ev.x.toInt()
                downY = ev.y.toInt()
            }
            MotionEvent.ACTION_MOVE -> {
                val moveX = ev.x.toInt()
                val diffX = moveX - downX
                val moveY = ev.y.toInt()
                val diffY = moveY - downY
                val condition = Math.abs(diffY) > Math.abs(diffX)
                return if (condition) {
                    downX = moveX
                    downY = moveY
                    true
                } else {
                    downX = moveX
                    downY = moveY
                    super.onInterceptTouchEvent(ev)
                }
            }
        }
        return super.onInterceptTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        performClick()
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                Log.d(TAG, "MotionEvent.ACTION_DOWN")
                flagDownX = event.x.toInt()
                flagDownY = event.y.toInt()
                downX = flagDownX
                downY = flagDownY
            }
            MotionEvent.ACTION_MOVE -> {
                Log.d(TAG, "MotionEvent.ACTION_MOVE")
                val moveX = event.x.toInt()
                val diffX = moveX - downX
                val moveY = event.y.toInt()
                val diffY = moveY - downY
                val condition = Math.abs(diffY) > Math.abs(diffX)
                if (condition) {
                    if (diffY < 0 && contentViewGroup?.hideContentView()!!) {
                        parent.requestDisallowInterceptTouchEvent(false)
                    } else {
                        contentViewGroup?.moveMoveLayout(diffY > 0, diffY)
                    }
                }
                downX = moveX
                downY = moveY
            }
            MotionEvent.ACTION_UP -> {
                Log.d(TAG, "MotionEvent.ACTION_UP")
                /*
                * 此处只判断y方向上的值,为了确保内容视图正确抬起与复原.
                * */
                //val upX = event.x.toInt()
                val upY = event.y.toInt()
                //val diffX = upX - flagDownX
                val diffY = upY - flagDownY
                //val condition = Math.abs(diffY) > Math.abs(diffX)
                //if (condition) {
                    contentViewGroup?.upMoveLayout(diffY > 0)
                //}
            }
        }
        return true
    }

    override fun performClick(): Boolean {
        return super.performClick()
    }

    fun attachContentViewGroup(contentViewGroup: TopContentViewGroup?) {
        this.contentViewGroup = contentViewGroup
    }

    companion object {
        const val TAG = "TopContentLayoutBottom"
    }

}